home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_crayon.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.3 KB  |  96 lines

  1. /* Renamed shader to PQCrayon for RMR -- talrmr@SpamSucks_pacbell.net */
  2.  
  3. /* crayon.sl - a surface shader making crayon like marks
  4.  
  5.     DESCRIPTION
  6.  
  7.     This shader makes the surface look as if it had been shaded with a pastel crayon.
  8.     It makes an attempt at antaliasing.
  9.     
  10.     PARAMETERS
  11.     
  12.     Ka, Kd, Ks, roughness, specularcolor - work as in the plastic shader
  13.     txtscale  - an overall scaling factor
  14.     width - the width of the crayon strokes - this is scaled by txtscale
  15.     micro - the size of the dots that make up a crayon stroke, relative to the size of
  16.         the stroke. By default they are about 15 times smaller
  17.     stretch - the length of the stroke relative to its width;
  18.     density0 - controls the amount of topcolor seen - measured as a proportion - 
  19.         this should vary between 0 (no topcolor) to 1, .3 would give 30% topcolor;
  20.     density1 - if different density0 this is the density when t = 1, with a smooth
  21.         interpolation of values for density0 when t = 0, thus allowing a 
  22.         graduation of shading from top to bottom of the object
  23.     color topcolor, basecolor - the color of the crayon strokes and the color of the ground
  24.     
  25.     AUTHOR
  26.     Peter Quint - Revised Monday, January 10, 2000 
  27.  
  28.  */
  29.  
  30.  
  31.  
  32. #define snoise(x,y) ((noise(x,y) - 0.5) * 2)
  33.  
  34. float
  35. aanoise(float sp, tp, width)
  36. {
  37.     /* an antaliased noise function, which returns noise of a wavelenth always greater than
  38.        twice the micropolygon width */
  39.     float f, mag, ns;
  40.     /* calculate smallest integer f for which width / f < .5 */
  41.     f = ceil(width /.5);
  42.     mag = max(pow(0.85, f - 1),.1);
  43.     /*(printf("f = %f, mag = %f\n",f,mag)*/;
  44.     ns  = mag * snoise(sp / f, tp / f) * (1 - smoothstep(0, .5, width / f))
  45.         + snoise(sp / (f * 1.33), tp / (f * 1.33)) * mag * .25 * smoothstep(0, .5, width / f);
  46.     return ns;
  47. }
  48.  
  49.  
  50. #define MINFILTWIDTH 1.0e-6
  51. #define filterwidth(x)  max (abs(Du(x)*du) + abs(Dv(x)*dv), MINFILTWIDTH)
  52.  
  53.  
  54. surface
  55. k3d_crayon (    float Ka = 1;
  56.             float Kd = .5;
  57.              float Ks = .5;
  58.              float roughness = .1;
  59.              color specularcolor = 1;
  60.             float txtscale = 1;
  61.             float width = .05;
  62.             float micro = 15.32;
  63.             float stretch = 10;
  64.             float density0 = .5;
  65.             float density1 = .5;
  66.             color topcolor = 1;
  67.             color basecolor = 0;
  68.         )
  69. {
  70.   color Csurf;
  71.     float density = density0 + t * (density1 - density0);
  72.     /* work out the density for the current t */
  73.     float trs = spline(1 - density, 0 , -0.195997, -0.128361, -0.0738346,    -0.0316483,
  74.             0.00807387,    0.0445915, 0.084543, 0.150693, 0.2198, 0.527474);
  75.     /*  use a spline to read across to the appropriate noise value - this equalisation
  76.         process is described by Steven Worley in Ch 3 of "Texturing and Modelling a
  77.         procedural approach */ 
  78.     normal Nf = faceforward (normalize(N),I);
  79.     float m;
  80.     float fw = max(filterwidth(s), filterwidth(t)); /* the size of the micropolygon */
  81.     float smks = aanoise(txtscale * s * micro / width, txtscale * t * micro / width,
  82.              txtscale * fw * micro / width);
  83.     float lmks = (aanoise(txtscale * s / width, txtscale * t / (width * stretch), 
  84.         txtscale * fw / width) + 1) / 2;
  85.     smks = (smks + 1) / 2;
  86.     lmks = lmks - smks;
  87.     m = smoothstep(trs - .1, trs + .3, lmks);    
  88.     m = clamp(m, 0, 1);
  89.     Csurf = mix(basecolor, topcolor, m);
  90.     Oi = Os;
  91.     Ci = Os * ( Csurf * (Ka*ambient() + Kd*diffuse(Nf)) +
  92.         specularcolor * Ks*specular(Nf,-normalize(I),roughness));
  93. }
  94.  
  95.  
  96.